package com.ourteam.product.ui.dao.impl;

import com.ourteam.product.ui.dao.IProductUITableColumnDAO;
import com.ourteam.product.ui.dao.ProductUITableColumn;
import com.ourteam.product.ui.dao.ProductUITableColumnDAOQueryBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.RowMapper;

/**
 * Template Name : DAO服务对象 Template Version : 1 Template Last Modfiy Date : Fri Nov 17 11:00:11 CST
 * 2017 Auto Generate Date : 2018-12-14 15:15:18 Warning ! This file will be overwrite when
 * regenerate !
 */
public class AbstractProductUITableColumnDAOJdbcImpl extends AbstractSpringDAO
    implements IProductUITableColumnDAO {

  public static final String[] ENTITY_FIELDS =
      new String[] {
        Id,
        TableId,
        Code,
        Title,
        TitleKey,
        TitleIcon,
        Width,
        Align,
        FormatValue,
        ListDataSource,
        CellEditor,
        DataType,
        ShowValue,
        CanSort,
        CanResize,
        CanFilter,
        CanReOrder,
        Frozen,
        SortIndex,
        LastModifyUser,
        LastModifyTime,
        Remarks,
        Status,
        ModelPropertyId,
        ActionGroupId,
        ListDataSourceCode,
        ListDataSourceName,
        ActionGroupCode
      };

  public static final String[] ENTITY_PROPERTIES =
      new String[] {
        _DataObjectProperty.Id,
        _DataObjectProperty.TableId,
        _DataObjectProperty.Code,
        _DataObjectProperty.Title,
        _DataObjectProperty.TitleKey,
        _DataObjectProperty.TitleIcon,
        _DataObjectProperty.Width,
        _DataObjectProperty.Align,
        _DataObjectProperty.FormatValue,
        _DataObjectProperty.ListDataSource,
        _DataObjectProperty.CellEditor,
        _DataObjectProperty.DataType,
        _DataObjectProperty.ShowValue,
        _DataObjectProperty.CanSort,
        _DataObjectProperty.CanResize,
        _DataObjectProperty.CanFilter,
        _DataObjectProperty.CanReOrder,
        _DataObjectProperty.Frozen,
        _DataObjectProperty.SortIndex,
        _DataObjectProperty.LastModifyUser,
        _DataObjectProperty.LastModifyTime,
        _DataObjectProperty.Remarks,
        _DataObjectProperty.Status,
        _DataObjectProperty.ModelPropertyId,
        _DataObjectProperty.ActionGroupId,
        _DataObjectProperty.ListDataSourceCode,
        _DataObjectProperty.ListDataSourceName,
        _DataObjectProperty.ActionGroupCode
      };

  public static final String[] ENTITY_PK_FIELDS = new String[] {Id};

  public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] {Id};

  public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {};

  private static String relatedJoinSQL = null;

  private static String basicFilterSQL = null;

  private DataAccessObjectHelper<ProductUITableColumn, ProductUITableColumnDAOQueryBean> daoHelper;

  public AbstractProductUITableColumnDAOJdbcImpl() {
    super();
    daoHelper =
        new DataAccessObjectHelper<ProductUITableColumn, ProductUITableColumnDAOQueryBean>(this);
  }

  protected String getEntityName() {
    return EntityName;
  }

  protected String[] getEntityFields() {
    return ENTITY_FIELDS;
  }

  protected String[] getEntityProperties() {
    return ENTITY_PROPERTIES;
  }

  protected String[] getEntityPKFields() {
    return ENTITY_PK_FIELDS;
  }

  protected String[] getEntityAutoGenPKFields() {
    return ENTITY_AUTO_GEN_PK_FIELDS;
  }

  protected String[] getEntityUniqueFields() {
    return ENTITY_UNIQUE_FIELDS;
  }

  protected String getBaiscQueryFilter() {

    if (basicFilterSQL == null) {
      StringBuffer basicFilterSQLbuffer = new StringBuffer();

      basicFilterSQL = basicFilterSQLbuffer.toString();
    }

    return basicFilterSQL;
  }

  protected String getRelatedJoinSQL() {

    if (relatedJoinSQL == null) {
      StringBuffer relatedJoinSQLBuffer = new StringBuffer();

      relatedJoinSQLBuffer
          .append(" LEFT JOIN ")
          .append(com.ourteam.product.ui.dao.IProductUIListDAO.EntityName)
          .append(" RELATED_K37FQJ_ProductUIList ");

      relatedJoinSQLBuffer.append(" ON ( ");

      relatedJoinSQLBuffer.append(
          StringUtils.replace(
              com.ourteam.product.ui.dao.IProductUIListDAO.Id,
              com.ourteam.product.ui.dao.IProductUIListDAO.EntityName + ".",
              "RELATED_K37FQJ_ProductUIList."));
      relatedJoinSQLBuffer.append(" = ").append(ListDataSource);

      relatedJoinSQLBuffer.append(" ) ");

      relatedJoinSQLBuffer
          .append(" LEFT JOIN ")
          .append(com.ourteam.product.ui.dao.IProductUIActionGroupDAO.EntityName)
          .append(" RELATED_DEJALW_ProductUIActionGroup ");

      relatedJoinSQLBuffer.append(" ON ( ");

      relatedJoinSQLBuffer.append(
          StringUtils.replace(
              com.ourteam.product.ui.dao.IProductUIActionGroupDAO.Id,
              com.ourteam.product.ui.dao.IProductUIActionGroupDAO.EntityName + ".",
              "RELATED_DEJALW_ProductUIActionGroup."));
      relatedJoinSQLBuffer.append(" = ").append(ActionGroupId);

      relatedJoinSQLBuffer.append(" ) ");

      relatedJoinSQL = relatedJoinSQLBuffer.toString();
    }

    return relatedJoinSQL;
  }

  /**
   * Check Data Unique
   *
   * @param aProductUITableColumn
   * @throws Exception
   */
  public boolean checkIsUnique(ProductUITableColumn aProductUITableColumn) throws Exception {

    if (aProductUITableColumn == null) {
      return true;
    }

    List valueList = new ArrayList();

    List pkValueList = new ArrayList();

    pkValueList.add(aProductUITableColumn.getId());

    return isUnique(
        valueList.toArray(new Object[valueList.size()]),
        pkValueList.toArray(new Object[pkValueList.size()]));
  }

  /**
   * Insert Data
   *
   * @param aProductUITableColumn
   * @throws Exception
   */
  public void insert(ProductUITableColumn aProductUITableColumn) throws Exception {

    PropertyUtils.setProperty(
        aProductUITableColumn,
        _DataObjectProperty.LastModifyTime,
        new java.sql.Timestamp(System.currentTimeMillis()));

    super.doInsert(aProductUITableColumn);
  }

  /**
   * Batch Insert Datas
   *
   * @param aProductUITableColumn
   * @throws Exception
   */
  public void batchInsert(ProductUITableColumn[] aProductUITableColumns) throws Exception {

    super.doBatchInsert(aProductUITableColumns);
  }

  /**
   * Delete Data
   *
   * @param final long id *
   * @throws Exception
   */
  public void delete(final long id) throws Exception {

    super.doDeleteById(new Object[] {new Long(id)});
  }

  /**
   * Delete Datas
   *
   * @param queryBean
   * @throws Exception
   */
  public void delete(ProductUITableColumnDAOQueryBean queryBean) throws Exception {

    super.doDeleteByQuery(queryBean);
  }

  public void delete(ProductUITableColumn aProductUITableColumn) throws Exception {

    delete(aProductUITableColumn.getId());
  }

  /**
   * Update Data Selective
   *
   * @param aProductUITableColumn
   * @throws Exception
   */
  public void update(ProductUITableColumn aProductUITableColumn) throws Exception {

    PropertyUtils.setProperty(
        aProductUITableColumn,
        _DataObjectProperty.LastModifyTime,
        new java.sql.Timestamp(System.currentTimeMillis()));

    if (aProductUITableColumn.getId() == 0) {
      throw new Exception("Can not update object data whith out primary key value");
    }

    super.doUpdate(aProductUITableColumn);

    ProductUITableColumn tempObj = findById(aProductUITableColumn.getId());

    if (tempObj != null) {
      PropertyUtils.copyProperties(aProductUITableColumn, tempObj);
    }
  }

  /**
   * Batch Update Datas
   *
   * @param aProductUITableColumns[]
   * @throws Exception
   */
  public void batchUpdate(ProductUITableColumn[] aProductUITableColumns) throws Exception {

    super.doBatchUpdate(aProductUITableColumns);
  }

  /**
   * Update Data By Condictions
   *
   * @param aProductUITableColumn
   * @param aValueCriteria
   * @param aTransaction
   * @throws Exception
   */
  public void update(
      ProductUITableColumn aProductUITableColumn, ProductUITableColumnDAOQueryBean aQueryBean)
      throws Exception {

    PropertyUtils.setProperty(
        aProductUITableColumn,
        _DataObjectProperty.LastModifyTime,
        new java.sql.Timestamp(System.currentTimeMillis()));

    super.doUpdateByQuery(aProductUITableColumn, aQueryBean);
  }

  /**
   * Update Data By Condictions
   *
   * @param aQueryBean
   * @throws Exception
   */
  public void update(ProductUITableColumnDAOQueryBean aQueryBean) throws Exception {
    super.doUpdateByQuery(aQueryBean);
  }

  protected void checkNullValue(ProductUITableColumn aProductUITableColumn) throws Exception {

    if (aProductUITableColumn.getCode() == null) {
      throw new Exception("The ProductUITableColumn.code value is not allow NULL");
    }

    if (aProductUITableColumn.getTitle() == null) {
      throw new Exception("The ProductUITableColumn.title value is not allow NULL");
    }

    if (aProductUITableColumn.getTitleKey() == null) {
      throw new Exception("The ProductUITableColumn.titleKey value is not allow NULL");
    }

    if (aProductUITableColumn.getTitleIcon() == null) {
      throw new Exception("The ProductUITableColumn.titleIcon value is not allow NULL");
    }

    if (aProductUITableColumn.getWidth() == null) {
      throw new Exception("The ProductUITableColumn.width value is not allow NULL");
    }

    if (aProductUITableColumn.getAlign() == null) {
      throw new Exception("The ProductUITableColumn.align value is not allow NULL");
    }

    if (aProductUITableColumn.getFormatValue() == null) {
      throw new Exception("The ProductUITableColumn.formatValue value is not allow NULL");
    }

    if (aProductUITableColumn.getListDataSource() == null) {
      throw new Exception("The ProductUITableColumn.listDataSource value is not allow NULL");
    }

    if (aProductUITableColumn.getCellEditor() == null) {
      throw new Exception("The ProductUITableColumn.cellEditor value is not allow NULL");
    }

    if (aProductUITableColumn.getDataType() == null) {
      throw new Exception("The ProductUITableColumn.dataType value is not allow NULL");
    }

    if (aProductUITableColumn.getShowValue() == null) {
      throw new Exception("The ProductUITableColumn.showValue value is not allow NULL");
    }

    if (aProductUITableColumn.getCanSort() == null) {
      throw new Exception("The ProductUITableColumn.canSort value is not allow NULL");
    }

    if (aProductUITableColumn.getCanResize() == null) {
      throw new Exception("The ProductUITableColumn.canResize value is not allow NULL");
    }

    if (aProductUITableColumn.getCanFilter() == null) {
      throw new Exception("The ProductUITableColumn.canFilter value is not allow NULL");
    }

    if (aProductUITableColumn.getCanReOrder() == null) {
      throw new Exception("The ProductUITableColumn.canReOrder value is not allow NULL");
    }

    if (aProductUITableColumn.getFrozen() == null) {
      throw new Exception("The ProductUITableColumn.frozen value is not allow NULL");
    }

    if (aProductUITableColumn.getLastModifyUser() == null) {
      throw new Exception("The ProductUITableColumn.lastModifyUser value is not allow NULL");
    }

    if (aProductUITableColumn.getLastModifyTime() == null) {
      throw new Exception("The ProductUITableColumn.lastModifyTime value is not allow NULL");
    }

    if (aProductUITableColumn.getRemarks() == null) {
      throw new Exception("The ProductUITableColumn.remarks value is not allow NULL");
    }

    if (aProductUITableColumn.getStatus() == null) {
      throw new Exception("The ProductUITableColumn.status value is not allow NULL");
    }
  }

  /**
   * Get Data By ID
   *
   * @param final long id * @return ${businessObject.name}
   * @throws Exception
   */
  public ProductUITableColumn findById(final long id) throws Exception {

    ProductUITableColumnDAOQueryBean queryBean = new ProductUITableColumnDAOQueryBean();

    ProductUITableColumnDAOQueryBean.Criteria criteria = queryBean.createCriteria();

    criteria.andIdEqualTo(id);

    // queryBean.setDistinct(true);

    ProductUITableColumn[] ProductUITableColumns = query(queryBean);

    if (ProductUITableColumns.length == 0) {
      return null;
    } else {
      return ProductUITableColumns[0];
    }
  }

  /**
   * Query Data
   *
   * @param queryBean
   * @return ProductUITableColumn[]
   * @throws Exception
   */
  public ProductUITableColumn[] query(ProductUITableColumnDAOQueryBean queryBean) throws Exception {

    List result = super.doQuery(queryBean, new JdbcProductUITableColumnRowMapper());

    return (ProductUITableColumn[]) result.toArray(new ProductUITableColumn[result.size()]);
  }

  /**
   * Query ${businessObject.name} Selective
   *
   * @param queryBean
   * @return Map[]
   * @model
   * @throws Exception
   */
  public Map<String, Object>[] querySelective(ProductUITableColumnDAOQueryBean queryBean)
      throws Exception {

    if (queryBean.getSelectProperties().isEmpty()) {
      queryBean.addAllSelectProperties();
    }

    return super.doQuerySelective(queryBean);
  }

  /**
   * Query Data Count
   *
   * @param queryBean
   * @return int
   * @throws Exception
   */
  public int queryCount(ProductUITableColumnDAOQueryBean queryBean) throws Exception {

    return super.doQueryCount(queryBean);
  }

  public void synchronize(
      ProductUITableColumn[] aProductUITableColumns, ProductUITableColumnDAOQueryBean queryBean)
      throws Exception {
    daoHelper.synchronize(aProductUITableColumns, queryBean);
  }

  public void synchronize(
      ProductUITableColumn[] aProductUITableColumns,
      ProductUITableColumnDAOQueryBean queryBean,
      net.dao.IDataAccessInterface.ISynchronizeCallback<ProductUITableColumn> callBack)
      throws Exception {
    daoHelper.synchronize(aProductUITableColumns, queryBean, callBack);
  }

  public static class JdbcProductUITableColumnRowMapper implements RowMapper<ProductUITableColumn> {

    public ProductUITableColumn mapRow(ResultSet resultSet, int arg1) throws SQLException {
      ProductUITableColumn object = new ProductUITableColumn();

      int columnIndex = 1;

      int __dataType;

      try {

        Object id = null;

        id = resultSet.getObject(columnIndex++);

        object.setIdNull(resultSet.getBoolean(columnIndex++));

        if (id != null) {

          setBeanValue(object, _DataObjectProperty.Id, id);
        }

        Object tableId = null;

        tableId = resultSet.getObject(columnIndex++);

        object.setTableIdNull(resultSet.getBoolean(columnIndex++));

        if (tableId != null) {

          setBeanValue(object, _DataObjectProperty.TableId, tableId);
        }

        Object code = null;

        code = resultSet.getObject(columnIndex++);

        object.setCodeNull(resultSet.getBoolean(columnIndex++));

        if (code != null) {

          setBeanValue(object, _DataObjectProperty.Code, code);
        }

        Object title = null;

        title = resultSet.getObject(columnIndex++);

        object.setTitleNull(resultSet.getBoolean(columnIndex++));

        if (title != null) {

          setBeanValue(object, _DataObjectProperty.Title, title);
        }

        Object titleKey = null;

        titleKey = resultSet.getObject(columnIndex++);

        object.setTitleKeyNull(resultSet.getBoolean(columnIndex++));

        if (titleKey != null) {

          setBeanValue(object, _DataObjectProperty.TitleKey, titleKey);
        }

        Object titleIcon = null;

        titleIcon = resultSet.getObject(columnIndex++);

        object.setTitleIconNull(resultSet.getBoolean(columnIndex++));

        if (titleIcon != null) {

          setBeanValue(object, _DataObjectProperty.TitleIcon, titleIcon);
        }

        Object width = null;

        width = resultSet.getObject(columnIndex++);

        object.setWidthNull(resultSet.getBoolean(columnIndex++));

        if (width != null) {

          setBeanValue(object, _DataObjectProperty.Width, width);
        }

        Object align = null;

        align = resultSet.getObject(columnIndex++);

        object.setAlignNull(resultSet.getBoolean(columnIndex++));

        if (align != null) {

          setBeanValue(object, _DataObjectProperty.Align, align);
        }

        Object formatValue = null;

        formatValue = resultSet.getObject(columnIndex++);

        object.setFormatValueNull(resultSet.getBoolean(columnIndex++));

        if (formatValue != null) {

          setBeanValue(object, _DataObjectProperty.FormatValue, formatValue);
        }

        Object listDataSource = null;

        listDataSource = resultSet.getObject(columnIndex++);

        object.setListDataSourceNull(resultSet.getBoolean(columnIndex++));

        if (listDataSource != null) {

          setBeanValue(object, _DataObjectProperty.ListDataSource, listDataSource);
        }

        Object cellEditor = null;

        cellEditor = resultSet.getObject(columnIndex++);

        object.setCellEditorNull(resultSet.getBoolean(columnIndex++));

        if (cellEditor != null) {

          setBeanValue(object, _DataObjectProperty.CellEditor, cellEditor);
        }

        Object dataType = null;

        dataType = resultSet.getObject(columnIndex++);

        object.setDataTypeNull(resultSet.getBoolean(columnIndex++));

        if (dataType != null) {

          setBeanValue(object, _DataObjectProperty.DataType, dataType);
        }

        Object showValue = null;

        showValue = resultSet.getObject(columnIndex++);

        object.setShowValueNull(resultSet.getBoolean(columnIndex++));

        if (showValue != null) {

          setBeanValue(object, _DataObjectProperty.ShowValue, showValue);
        }

        Object canSort = null;

        canSort = resultSet.getObject(columnIndex++);

        object.setCanSortNull(resultSet.getBoolean(columnIndex++));

        if (canSort != null) {

          setBeanValue(object, _DataObjectProperty.CanSort, canSort);
        }

        Object canResize = null;

        canResize = resultSet.getObject(columnIndex++);

        object.setCanResizeNull(resultSet.getBoolean(columnIndex++));

        if (canResize != null) {

          setBeanValue(object, _DataObjectProperty.CanResize, canResize);
        }

        Object canFilter = null;

        canFilter = resultSet.getObject(columnIndex++);

        object.setCanFilterNull(resultSet.getBoolean(columnIndex++));

        if (canFilter != null) {

          setBeanValue(object, _DataObjectProperty.CanFilter, canFilter);
        }

        Object canReOrder = null;

        canReOrder = resultSet.getObject(columnIndex++);

        object.setCanReOrderNull(resultSet.getBoolean(columnIndex++));

        if (canReOrder != null) {

          setBeanValue(object, _DataObjectProperty.CanReOrder, canReOrder);
        }

        Object frozen = null;

        frozen = resultSet.getObject(columnIndex++);

        object.setFrozenNull(resultSet.getBoolean(columnIndex++));

        if (frozen != null) {

          setBeanValue(object, _DataObjectProperty.Frozen, frozen);
        }

        Object sortIndex = null;

        sortIndex = resultSet.getObject(columnIndex++);

        object.setSortIndexNull(resultSet.getBoolean(columnIndex++));

        if (sortIndex != null) {

          setBeanValue(object, _DataObjectProperty.SortIndex, sortIndex);
        }

        Object lastModifyUser = null;

        lastModifyUser = resultSet.getObject(columnIndex++);

        object.setLastModifyUserNull(resultSet.getBoolean(columnIndex++));

        if (lastModifyUser != null) {

          setBeanValue(object, _DataObjectProperty.LastModifyUser, lastModifyUser);
        }

        Object lastModifyTime = null;

        __dataType = resultSet.getMetaData().getColumnType(columnIndex);

        if (__dataType == java.sql.Types.DATE
            || __dataType == java.sql.Types.TIME
            || __dataType == java.sql.Types.TIMESTAMP) {
          lastModifyTime = resultSet.getTimestamp(columnIndex++);

        } else {
          lastModifyTime = resultSet.getObject(columnIndex++);
        }

        object.setLastModifyTimeNull(resultSet.getBoolean(columnIndex++));

        if (lastModifyTime != null) {

          setBeanValue(object, _DataObjectProperty.LastModifyTime, lastModifyTime);
        }

        Object remarks = null;

        remarks = resultSet.getObject(columnIndex++);

        object.setRemarksNull(resultSet.getBoolean(columnIndex++));

        if (remarks != null) {

          setBeanValue(object, _DataObjectProperty.Remarks, remarks);
        }

        Object status = null;

        status = resultSet.getObject(columnIndex++);

        object.setStatusNull(resultSet.getBoolean(columnIndex++));

        if (status != null) {

          setBeanValue(object, _DataObjectProperty.Status, status);
        }

        Object modelPropertyId = null;

        modelPropertyId = resultSet.getObject(columnIndex++);

        object.setModelPropertyIdNull(resultSet.getBoolean(columnIndex++));

        if (modelPropertyId != null) {

          setBeanValue(object, _DataObjectProperty.ModelPropertyId, modelPropertyId);
        }

        Object actionGroupId = null;

        actionGroupId = resultSet.getObject(columnIndex++);

        object.setActionGroupIdNull(resultSet.getBoolean(columnIndex++));

        if (actionGroupId != null) {

          setBeanValue(object, _DataObjectProperty.ActionGroupId, actionGroupId);
        }

        Object listDataSourceCode = null;

        listDataSourceCode = resultSet.getObject(columnIndex++);

        object.setListDataSourceCodeNull(resultSet.getBoolean(columnIndex++));

        if (listDataSourceCode != null) {

          setBeanValue(object, _DataObjectProperty.ListDataSourceCode, listDataSourceCode);
        }

        Object listDataSourceName = null;

        listDataSourceName = resultSet.getObject(columnIndex++);

        object.setListDataSourceNameNull(resultSet.getBoolean(columnIndex++));

        if (listDataSourceName != null) {

          setBeanValue(object, _DataObjectProperty.ListDataSourceName, listDataSourceName);
        }

        Object actionGroupCode = null;

        actionGroupCode = resultSet.getObject(columnIndex++);

        object.setActionGroupCodeNull(resultSet.getBoolean(columnIndex++));

        if (actionGroupCode != null) {

          setBeanValue(object, _DataObjectProperty.ActionGroupCode, actionGroupCode);
        }

        return object;
      } catch (Exception e) {
        throw new SQLException(e.getMessage());
      }
    }
  }
}
